package com.house365.core.adapter;

import java.util.ArrayList;
import java.util.List;

import com.house365.core.bean.Group;


/**
 *@author py
 *@date 2012-6-12
 *@comment
 */
public abstract class BaseExpandableListAdapter<T,R> extends android.widget.BaseExpandableListAdapter {
	protected final List<Group<T,R>> list;
	public BaseExpandableListAdapter() {
		list = new ArrayList<Group<T,R>>();
	}
	@Override
	public int getGroupCount() {
		return list.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return list.get(groupPosition).getSubsCount();
	}

	@Override
	public T getGroup(int groupPosition) {
		return list.get(groupPosition).getGroup();
	}

	@Override
	public R getChild(int groupPosition, int childPosition) {
		return list.get(groupPosition).getSub(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}


	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	public void clear() {
		this.list.clear();
	}

	public void addAllGroup(List<? extends Group<T,R>> tlist) {
		this.list.addAll(tlist);
	}
	public void addGroup(List<? extends T> tlist){
		for(T t:tlist){
			this.list.add(new Group<T,R>(t));
		}
	}
	public void addGroup(T t){
			this.list.add(new Group<T,R>(t));
	}
	public void addSubs(int groupPosition,List<? extends R> tlist){
		this.list.get(groupPosition).addSubs(tlist);
	}
	public void addSub(int groupPosition,R r){
		this.list.get(groupPosition).addSub(r);
	}
	public Group remove(int groupPosition) {
		return this.list.remove(groupPosition);
	}

}
